/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on 06.04.2009, 22:48:22
 */

package ru.vsu.cs.piit.vokod.view;

import java.awt.Color;
import ru.vsu.cs.piit.vokod.view.panels.ReportsPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.metal.MetalLookAndFeel;
import ru.vsu.cs.piit.vokod.IListener;
import ru.vsu.cs.piit.vokod.ObservableArrayList;
import ru.vsu.cs.piit.vokod.TableModels.MoveTable;
import ru.vsu.cs.piit.vokod.TableModels.PacientsTable;
import ru.vsu.cs.piit.vokod.controller.Controller;
import ru.vsu.cs.piit.vokod.model.DailyMove;
import ru.vsu.cs.piit.vokod.model.Pacient;

/**
 *
 * @author Галчонок
 */
public class MainFrame extends javax.swing.JFrame {

    public MainFrame(ObservableArrayList<DailyMove> list, ObservableArrayList<Pacient> plist) {
        initComponents(list, plist);
    }

    @SuppressWarnings("unchecked")
    private void initComponents(ObservableArrayList<DailyMove> list, ObservableArrayList<Pacient> plist) {
        try {
            UIManager.setLookAndFeel(new MetalLookAndFeel());
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jScrollPane3 = new javax.swing.JScrollPane();
        jToolBar = new javax.swing.JToolBar();
        moveTable = new MoveTable(list);
        jAddButton = new javax.swing.JToggleButton();
        jEditButton = new javax.swing.JToggleButton();
        jDelButton = new JButton();
        pacientsTable = new PacientsTable(plist);

        reportsPanel = new ReportsPanel();
        reportsPanel.AddListener(new IListener() {

            @Override
            public void DoEvent() {
                mf.repaint();
            }
        });
        
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTabbedPane1.addTab("Движение", jScrollPane1);
        jTabbedPane1.addTab("Пациенты", jScrollPane2);
        jTabbedPane1.addTab("Отчеты", jScrollPane3);

        this.jScrollPane1.setBorder(new EmptyBorder(5, 5, 5, 5));
        this.jScrollPane2.setBorder(new EmptyBorder(5, 5, 5, 5));
        this.jScrollPane3.setBorder(new EmptyBorder(5, 5, 5, 5));

        this.jScrollPane2.setBackground(Color.WHITE);
        
        jAddButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ru/vsu/cs/piit/vokod/resources/add.gif")));
        jAddButton.setFocusable(false);
        jToolBar.add(jAddButton);

        jEditButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ru/vsu/cs/piit/vokod/resources/change.gif")));
        jEditButton.setFocusable(false);
        jToolBar.add(jEditButton);

        jDelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ru/vsu/cs/piit/vokod/resources/remove.gif")));
        jDelButton.setFocusable(false);
        jToolBar.add(jDelButton);

        jScrollPane1.setViewportView(moveTable);
        jScrollPane2.setViewportView(pacientsTable);
        jScrollPane3.setViewportView(reportsPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE)
            .addComponent(jToolBar, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE))
        );
        this.setTitle("VOKOD-DB");

        pack();
        setExtendedState(MainFrame.MAXIMIZED_BOTH);

        jTabbedPane1.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                if (jTabbedPane1.getSelectedIndex()>1){
                    jToolBar.setEnabled(false);
                    jAddButton.setEnabled(false);
                    jEditButton.setEnabled(false);
                    jDelButton.setEnabled(false);
                } else {
                    jToolBar.setEnabled(true);
                    jAddButton.setEnabled(true);
                    jEditButton.setEnabled(true);
                    jDelButton.setEnabled(true);
                }
            }
        });

        jAddButton.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (jTabbedPane1.getSelectedIndex()==0){
//                    if (jAddButton.isSelected()){
//                        moveTable.setAddMode(true);
//                    } else {
//                        moveTable.setAddMode(false);
//                    }
                    Controller.getInstance().addDailyMove();
                    jAddButton.setSelected(!jAddButton.isSelected());
                }
                else {
                    Controller.getInstance().addPacient();
                    jAddButton.setSelected(!jAddButton.isSelected());
                }
            }
        });


        jEditButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (jTabbedPane1.getSelectedIndex()==0)
//                    moveTable.setEditMode(true);
                    Controller.getInstance().editDailyMove(moveTable.getModel().getMove(moveTable.getSelectedRow()));
                else
                    Controller.getInstance().editPacient(pacientsTable.getModel().getPacient(pacientsTable.getSelectedRow()));
            }
        });

        jDelButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (jTabbedPane1.getSelectedIndex()==0)
                    moveTable.delCurrentRow();
                else
                    pacientsTable.delCurrentRow();
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JToolBar jToolBar;
    private javax.swing.JToggleButton jAddButton;
    private javax.swing.JToggleButton jEditButton;
    private javax.swing.JButton jDelButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private MoveTable moveTable;
    private PacientsTable pacientsTable;
    private ReportsPanel reportsPanel;
    private MainFrame mf = this;
    // End of variables declaration
}
